<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回一个包含结果集中所有行的数组</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="pdostatement.fetch.html">« PDOStatement::fetch</a></li>
      <li style="float: right;"><a href="pdostatement.fetchcolumn.html">PDOStatement::fetchColumn »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.pdostatement.html">PDOStatement</a></li>
    <li>返回一个包含结果集中所有行的数组</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="pdostatement.fetchall" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement::fetchAll</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDOStatement::fetchAll</span> &mdash; <span class="dc-title">
   返回一个包含结果集中所有行的数组
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdostatement.fetchall-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>PDOStatement::fetchAll</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$fetch_style</code><span class="initializer"> = ?</span></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$fetch_argument</code><span class="initializer"> = ?</span></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$ctor_args</code><span class="initializer"> = array()</span></span>): <span class="type">array</span></div>


 </div>


 <div class="refsect1 parameters" id="refsect1-pdostatement.fetchall-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">fetch_style</code></dt>

     <dd>

      <p class="para">
       控制返回数组的内容如同 <span class="function"><a href="pdostatement.fetch.html" class="function">PDOStatement::fetch()</a></span> 文档中记载的一样。默认为 <strong><code>PDO::ATTR_DEFAULT_FETCH_MODE</code></strong> 的值（ 其缺省值为 <strong><code>PDO::FETCH_BOTH</code></strong> ）
      </p>
      <p class="para">
       想要返回一个包含结果集中单独一列所有值的数组，需要指定 <strong><code>PDO::FETCH_COLUMN</code></strong> 。通过指定  <code class="parameter">column-index</code>  参数获取想要的列。
      </p>
      <p class="para">
      想要获取结果集中单独一列的唯一值，需要将 <strong><code>PDO::FETCH_COLUMN</code></strong> 和 <strong><code>PDO::FETCH_UNIQUE</code></strong> 按位或。
      </p>
      <p class="para">
       想要返回一个根据指定列把值分组后的关联数组，需要将 <strong><code>PDO::FETCH_COLUMN</code></strong> 和 <strong><code>PDO::FETCH_GROUP</code></strong> 按位或。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">fetch_argument</code></dt>

     <dd>

      <p class="para">
       根据 <code class="parameter">fetch_style</code> 参数的值，此参数有不同的意义：
       <ul class="itemizedlist">
        <li class="listitem">
         <p class="para">
          <strong><code>PDO::FETCH_COLUMN</code></strong>：返回指定以0开始索引的列。
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>PDO::FETCH_CLASS</code></strong>：返回指定类的实例，映射每行的列到类中对应的属性名。
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>PDO::FETCH_FUNC</code></strong>：将每行的列作为参数传递给指定的函数，并返回调用函数后的结果。
         </p>
        </li>
       </ul>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">ctor_args</code></dt>

     <dd>

      <p class="para">
       当 <code class="parameter">fetch_style</code> 参数为 <strong><code>PDO::FETCH_CLASS</code></strong> 时，自定义类的构造函数的参数。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdostatement.fetchall-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>PDOStatement::fetchAll()</strong></span> 返回一个包含结果集中所有剩余行的数组。此数组的每一行要么是一个列值的数组，要么是属性对应每个列名的一个对象。
  </p>
  <p class="para">
   使用此方法获取大结果集将导致系统负担加重且可能占用大量网络资源。与其取回所有数据后用PHP来操作，倒不如考虑使用数据库服务来处理结果集。例如，在取回数据并通过PHP处理前，在 SQL 中使用 WHERE  和 ORDER BY 子句来限定结果。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdostatement.fetchall-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1073"><p><strong>示例 #1 获取结果集中所有剩余的行</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$sth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;name,&nbsp;colour&nbsp;FROM&nbsp;fruit"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;获取结果集中所有剩余的行&nbsp;*/<br /></span><span style="color: #007700">print(</span><span style="color: #DD0000">"Fetch&nbsp;all&nbsp;of&nbsp;the&nbsp;remaining&nbsp;rows&nbsp;in&nbsp;the&nbsp;result&nbsp;set:\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Fetch all of the remaining rows in the result set:
Array
(
    [0] =&gt; Array
        (
            [NAME] =&gt; pear
            [0] =&gt; pear
            [COLOUR] =&gt; green
            [1] =&gt; green
        )

    [1] =&gt; Array
        (
            [NAME] =&gt; watermelon
            [0] =&gt; watermelon
            [COLOUR] =&gt; pink
            [1] =&gt; pink
        )

)
</pre></div>
    </div>
   </div>
   <div class="example" id="example-1074"><p><strong>示例 #2 获取结果集中单独一列的所有值</strong></p>
    <div class="example-contents"><p>
     下面例子演示了如何从一个结果集中返回单独一列所有的值，尽管 SQL 语句自身可能返回每行多列。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$sth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;name,&nbsp;colour&nbsp;FROM&nbsp;fruit"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;获取第一列所有值&nbsp;*/<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_COLUMN</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array(3)
(
    [0] =&gt;
    string(5) =&gt; apple
    [1] =&gt;
    string(4) =&gt; pear
    [2] =&gt;
    string(10) =&gt; watermelon
)
</pre></div>
    </div>
   </div>
   <div class="example" id="example-1075"><p><strong>示例 #3 根据单独的一列把所有值分组</strong></p>
    <div class="example-contents"><p>
     下面例子演示了如何返回一个根据结果集中指定列的值分组的关联数组。该数组包含三个键：返回的 <code class="literal">apple</code> 和 <code class="literal">pear</code> 数组包含了两种不同的颜色，而返回的 <code class="literal">watermelon</code> 数组仅包含一种颜色。 
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$insert&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;fruit(name,&nbsp;colour)&nbsp;VALUES&nbsp;(?,&nbsp;?)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$insert</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'green'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$insert</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'pear'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$sth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;name,&nbsp;colour&nbsp;FROM&nbsp;fruit"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;根据第一列分组&nbsp;&nbsp;*/<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_COLUMN</span><span style="color: #007700">|</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_GROUP</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [&quot;apple&quot;]=&gt;
  array(2) {
    [0]=&gt;
    string(5) &quot;green&quot;
    [1]=&gt;
    string(3) &quot;red&quot;
  }
  [&quot;pear&quot;]=&gt;
  array(2) {
    [0]=&gt;
    string(5) &quot;green&quot;
    [1]=&gt;
    string(6) &quot;yellow&quot;
  }
  [&quot;watermelon&quot;]=&gt;
  array(1) {
    [0]=&gt;
    string(5) &quot;green&quot;
  }
}

</pre></div>
    </div>
   </div>
   <div class="example" id="example-1076"><p><strong>示例 #4 每行结果实例化一个类</strong></p>
    <div class="example-contents"><p>
     下面列子演示了 <strong><code>PDO::FETCH_CLASS</code></strong> 获取风格的行为。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">fruit&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$colour</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$sth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;name,&nbsp;colour&nbsp;FROM&nbsp;fruit"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_CLASS</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"fruit"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [0]=&gt;
  object(fruit)#1 (2) {
    [&quot;name&quot;]=&gt;
    string(5) &quot;apple&quot;
    [&quot;colour&quot;]=&gt;
    string(5) &quot;green&quot;
  }
  [1]=&gt;
  object(fruit)#2 (2) {
    [&quot;name&quot;]=&gt;
    string(4) &quot;pear&quot;
    [&quot;colour&quot;]=&gt;
    string(6) &quot;yellow&quot;
  }
  [2]=&gt;
  object(fruit)#3 (2) {
    [&quot;name&quot;]=&gt;
    string(10) &quot;watermelon&quot;
    [&quot;colour&quot;]=&gt;
    string(4) &quot;pink&quot;
  }
}
</pre></div>
    </div>
   </div>
   <div class="example" id="example-1077"><p><strong>示例 #5  每行调用一次函数</strong></p>
    <div class="example-contents"><p>
     下面列子演示了 <strong><code>PDO::FETCH_FUNC</code></strong> 获取风格的行为。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">fruit</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$colour</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$name</span><span style="color: #007700">}</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$colour</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$sth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;name,&nbsp;colour&nbsp;FROM&nbsp;fruit"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_FUNC</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"fruit"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [0]=&gt;
  string(12) &quot;apple: green&quot;
  [1]=&gt;
  string(12) &quot;pear: yellow&quot;
  [2]=&gt;
  string(16) &quot;watermelon: pink&quot;
}
</pre></div>
    </div>
   </div>

  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdostatement.fetchall-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="pdo.query.html" class="function" rel="rdfs-seeAlso">PDO::query()</a> - 执行 SQL 语句，以 PDOStatement 对象形式返回结果集</span></li>
    <li class="member"><span class="function"><a href="pdostatement.fetch.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetch()</a> - 从结果集中获取下一行</span></li>
    <li class="member"><span class="function"><a href="pdostatement.fetchcolumn.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a> - 从结果集中的下一行返回单独的一列。</span></li>
    <li class="member"><span class="function"><a href="pdo.prepare.html" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - 准备要执行的语句，并返回语句对象</span></li>
    <li class="member"><span class="function"><a href="pdostatement.setfetchmode.html" class="function" rel="rdfs-seeAlso">PDOStatement::setFetchMode()</a> - 为语句设置默认的获取模式。</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>